#include <iostream>
#include <list>

#include "ui_cli.h"
#include "discinfo.h"
#include "client.h"

// ---------------------------------------------
void UiCli::log(const Ui::lvl_t lvl, const std::string from, const std::string what)
// ---------------------------------------------
{
	if (lvl == ERROR)
	{
		std::cerr << "[" << from << "] " << what << std::endl;
	}
	else
  if (lvl <= output_msglvl)
	{    
		std::cout << "[" << from << "] " << what << std::endl;
	}
}

// ---------------------------------------------
void UiCli::mainloop()
// ---------------------------------------------
{
	std::string input;
	
	std::cout << std::endl;
	std::cout << "CLI initialized. Available commands are:" << std::endl;
	std::cout << "  quit   - Terminate the application" << std::endl;
	std::cout << "  update - Fetch and set metadata about the currently inserted CD" << std::endl;
	std::cout << std::endl << "> ";

	while (std::getline(std::cin, input))
	{
		std::cout << std::endl;

		if (input.compare("quit") == 0) break;

		if (input.compare("update") == 0)
		{
			disc_t info;
			if ( !app.getDiscInfo(info) )
			{
				std::cout << "Could not obtain CD information." << std::endl;
				std::cout << std::endl << "> ";
				continue;
			}
			std::cout << "Obtained CD information." << std::endl;
			if (!app.xmmsUpdate(info))
			{
				std::cout << "Failed to update XMMS2 medialib." << std::endl;
				std::cout << std::endl << "> ";
				continue;
			}
      if (!info.save())
      {
				std::cout << "Failed to save CD information." << std::endl;
      }
			std::cout << "XMMS2 medialib updated." << std::endl;
		}
		std::cout << std::endl << "> ";
	}
}

// ---------------------------------------------
std::list<disc_t>::const_iterator UiCli::chooseDisc(const std::list<disc_t>& matches)
// ---------------------------------------------
{
	std::list<disc_t>::const_iterator i;
  
  // display the prompt
  std::cout << "Got " << matches.size() << " entries." << std::endl;
  std::cout << "Type in `#` to select the entry you want to use." << std::endl;
  std::cout << "Type in `show #` to display raw entry data." << std::endl;
  
  // enter interaction loop
  std::string userinput;
  unsigned int count;
  while (1)
  {
    count = 1;
    for (i=matches.begin(); i!=matches.end(); ++i)
    {
      std::cout << "(";
      std::cout << count;
      std::cout << ") " << i->title << std::endl;
      count++;
    }
    std::cout << "> ";
    userinput.clear();
    std::getline(std::cin, userinput);
    // parse input
    count = 0;
    if (userinput.compare(0, 5, "show ") == 0)
    {
      count = 254;
      userinput = userinput.substr(5);
    }
    for (std::string::iterator si=userinput.begin(); si!=userinput.end(); ++si)
    {
      if (*si <= 48 && *si >= 57) // non-digits
      {
        count = 255;
        break;
      }
    }
    if (count != 255)
    {
      int eint = atoi(userinput.data())-1;
      if (eint < matches.size())
      {
        if (count != 254)
        {
          // user input only the number, break the loop
          count = 1;
          for (i=matches.begin(); i!=matches.end(); ++i)
          {
            if (count == eint) break;
          }
          break;
        }
        else
        {
          // show entry data
          count = 1;
          for (i=matches.begin(); i!=matches.end(); ++i)
          {
            if (count == eint) break;
          }
          std::cout << "Disc Artist: " << i->artist << std::endl;
          std::cout << "Disc Title: " << i->title << std::endl;
          int trno = 1;
          for (std::vector<track_t>::const_iterator tr=i->tracks.begin(); tr!=i->tracks.end(); tr++)
          {
            std::cout << "Track ";
            if (trno < 10) std::cout << " ";
            std::cout << trno;
            std::cout << "Artist: " << tr->artist << std::endl;
            std::cout << "Track ";
            if (trno < 10) std::cout << " ";
            std::cout << trno;
            std::cout << "Title: " << tr->title << std::endl;
            trno++;
          }
        }
      }
    }
  }

	return i;
}
